package com.heima.wemedia.controller.v1;

import com.heima.common.constants.CacheConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/sensitive")
@RequiredArgsConstructor
public class WmSensitiveController {

    private final WmSensitiveService sensitiveService;

    /**
     * 敏感词分页
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public PageResponseResult sensitiveList(@RequestBody SensitiveDto dto){
        return sensitiveService.sensitiveList(dto);
    }

    /**
     * 添加敏感词
     * @param sensitive
     * @return
     */
    @PostMapping("/save")
    public ResponseResult saveSensitive(@RequestBody WmSensitive sensitive){
        return sensitiveService.saveSensitive(sensitive);
    }

    /**
     * 敏感词删除
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    @Caching(evict = {@CacheEvict(cacheNames = CacheConstants.SENSITIVE_PREFIX, allEntries = true)})
    public ResponseResult deleteSensitiveById(@PathVariable Integer id){
        return ResponseResult.okResult(sensitiveService.removeById(id));
    }

    /**
     * 敏感词修改
     * @param sensitive
     * @return
     */
    @PostMapping("/update")
    @Caching(evict = {@CacheEvict(cacheNames = CacheConstants.SENSITIVE_PREFIX, allEntries = true)})
    public ResponseResult updateSensitive(@RequestBody WmSensitive sensitive){
        return ResponseResult.okResult(sensitiveService.updateById(sensitive));
    }
}
